package www.nomina.model.config;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.sql.DataSource;

public class serviceLocator {

	private static serviceLocator instance = null;
	private InitialContext mycontext;
	private String strDataSource;
	private DataSource dataSource;

	/**
	 * Contructor de Clase
	 * qv4bA9tq9cZ2
	 */
	public serviceLocator() {

		try {
			contextConnection();
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
	}

	private void contextConnection() throws NameNotFoundException {
		try {
			mycontext = new InitialContext();
			strDataSource = "java:comp/env/jdbc/nomina";
			dataSource = (DataSource) mycontext.lookup(strDataSource);
			System.out.println(dataSource);
			System.out.println("Levantanto servicio MYSQL");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * implementacion del patron singleton par que esta clase solo exista una
	 * sola vez en todo el proyecto.
	 * 
	 * @return
	 */
	public static serviceLocator getInstance() {
		if (instance == null) {
			synchronized (serviceLocator.class) {
				if (instance == null) {
					instance = new serviceLocator();
				}
			}
		}
		return instance;
	}

	public Connection getMYSQLConnection() throws SQLException {
		return this.dataSource.getConnection();
	}
}
